package cn.com.jslh.nfcpro.base;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 作者：周洲 on 2017/3/8 09:38
 * <p>
 * 邮箱：double0zhou@126.com
 */

public abstract class BaseRecycleViewAdapter<T> extends RecyclerView.Adapter<SuperViewHolder> {
    private OnItemClickListener onItemClickListener;
    private OnItemClickListener onItemLongClickListener;
    private List<T> datalist = new ArrayList<>();

    @Override
    public SuperViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(), parent, false);
        return new SuperViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SuperViewHolder holder, final int position) {
        onBindItemHolder(holder, position);
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position);
                }
            });
        }
        if (onItemLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItemLongClickListener.onItemClick(v, position);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return datalist.size();
    }

    /**
     * 添加
     *
     * @param datalist
     */
    public void setDatalist(Collection<T> datalist) {
        if (this.datalist == null) {
            this.datalist = new ArrayList<>();
        }
        this.datalist.clear();
        this.datalist.addAll(datalist);
        notifyDataSetChanged();
    }

    public void addItem(T item) {
        if (this.datalist == null) {
            this.datalist = new ArrayList<>();
        }
        this.datalist.add(item);
        notifyDataSetChanged();
    }
    public void addItem(int index,T item) {
        if (this.datalist == null) {
            this.datalist = new ArrayList<>();
        }
        this.datalist.add(index,item);
        notifyDataSetChanged();
    }

    public List<T> getDatalist() {
        if (this.datalist == null) {
            this.datalist = new ArrayList<>();
        }
        return datalist;
    }

    public abstract int getLayoutId();

    public abstract void onBindItemHolder(SuperViewHolder holder, int position);


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
